package com.junlong0716.enjoylearning.ui

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.rxevent.RefreshEvent
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.module.common.rxbus.RxBus
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_add_notes.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject

class AddNotesActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_notes)
        tv_title.text = "添加笔记"
        rl_back.setOnClickListener { finish() }
        bt_confirm.setOnClickListener {
            val content = det_note_content.text.toString().trim()
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort("笔记内容不能为空！")
                return@setOnClickListener
            }


            val map = HashMap<String, Any>()
            map["userId"] = MyApp.instance().userAccount!!.id
            map["noteContent"] = content

            val jsonObj = JSONObject(map)
            OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_ADD_NOTE)
                    .tag(this)
                    .upJson(jsonObj)
                    .execute(object : StringCallback() {
                        override fun onSuccess(response: Response<String>?) {
                            val jsonObject = JSONObject(response!!.body())
                            val code = jsonObject.getString("code")
                            if (code == "200") {
                                ToastUtils.showShort("添加笔记成功！")
                                RxBus.getDefault().post(RefreshEvent(0xbbfa))
                                finish()
                            } else {

                            }
                        }
                    })
        }
    }
}
